package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.FlowerOrder;
import com.woniu.mapper.FlowerOrderMapper;
import com.woniu.service.FlowerOrderService;
import com.woniu.vo.FlowerOrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.List;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Service
@Slf4j
public class FlowerOrderServiceImpl implements FlowerOrderService {
    @Resource
    private FlowerOrderMapper flowerOrderMapper;

    @Override
    public PageInfo<FlowerOrderVo> selectFlowerOrderByMerchant
            (Integer pageNum, Integer pageSize, String newOrderId, String accountName,Integer status, Long startTime,Long endTime) {
        PageHelper.startPage(pageNum,pageSize);
        if (startTime != null && endTime != null) {
            Date startTimedate = new Date(startTime);
            Date endTimedate = new Date(endTime);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startTimedate1 = simpleDateFormat.format(startTimedate).substring(0,10)+" 00:00:00";

            String endTimedate1 = simpleDateFormat.format(endTimedate).substring(0,10)+" 23:59:59";
            List<FlowerOrderVo> list = flowerOrderMapper.selectFlowerOrderByMerchant(newOrderId, accountName,status,startTimedate1,endTimedate1);
            return new PageInfo<FlowerOrderVo>(list);
        }
        List<FlowerOrderVo> list = flowerOrderMapper.selectFlowerOrderByMerchant(newOrderId, accountName,status,null,null);
        PageInfo<FlowerOrderVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public FlowerOrderVo selectFlowerOrderByOrderId(String newOrderId) {
        return flowerOrderMapper.selectFlowerOrderByOrderId(newOrderId);
    }

    @Override
    public int updateFlowerOrderStatusByOrderId(FlowerOrder flowerOrder) {
        return flowerOrderMapper.updateByPrimaryKeySelective(flowerOrder);
    }

    @Override
    public PageInfo<FlowerOrderVo> selectNewFlowerOrder(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<FlowerOrderVo> list = flowerOrderMapper.selectNewFlowerOrder();
        PageInfo<FlowerOrderVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public FlowerOrderVo selectDayTotal() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String startTime = now.format(formatter).substring(0, 10) + " 00:00:00";
        String endTime = now.format(formatter).substring(0, 10) + " 23:59:59";
        FlowerOrderVo flowerOrderVo = flowerOrderMapper.selectTotal(startTime,endTime);
        if (flowerOrderVo ==null){
            FlowerOrderVo newFlowerOrderVo = new FlowerOrderVo();
            newFlowerOrderVo.setCount(BigDecimal.valueOf(0));
           return newFlowerOrderVo;
        }
        return flowerOrderVo;
    }

    @Override
    public FlowerOrderVo selectWeekTotal() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 获取本周一的日期时间
        LocalDateTime weekStart = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));

        // 获取下周一的日期时间，即当前周的最后一天
        LocalDateTime weekEnd = weekStart.plusDays(6).with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
        String startTime = weekStart.format(formatter).substring(0, 10) + " 00:00:00";
        String endTime = weekEnd.format(formatter).substring(0, 10) + " 23:59:59";
        FlowerOrderVo flowerOrderVo = flowerOrderMapper.selectTotal(startTime,endTime);
        if (flowerOrderVo ==null){
            FlowerOrderVo newFlowerOrderVo = new FlowerOrderVo();
            newFlowerOrderVo.setCount(BigDecimal.valueOf(0));
            return newFlowerOrderVo;
        }
        return flowerOrderVo;
    }

    @Override
    public FlowerOrderVo selectMonthTotal() {
        LocalDate now = LocalDate.now();

        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 获取本月月初
        LocalDate monthStart = now.with(TemporalAdjusters.firstDayOfMonth());

        // 获取本月月底
        LocalDate monthEnd = now.with(TemporalAdjusters.lastDayOfMonth());

        // 将 LocalDate 转换为 LocalDateTime，并设置时间为 00:00:00 和 23:59:59
        LocalDateTime startTime = LocalDateTime.of(monthStart, java.time.LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(monthEnd, java.time.LocalTime.MAX);

        // 格式化 LocalDateTime
        String startTimeFormatted = startTime.format(formatter) + " 00:00:00";
        String endTimeFormatted = endTime.format(formatter) + " 23:59:59";

        FlowerOrderVo flowerOrderVo = flowerOrderMapper.selectTotal(startTimeFormatted,endTimeFormatted);
        if (flowerOrderVo ==null){
            FlowerOrderVo newFlowerOrderVo = new FlowerOrderVo();
            newFlowerOrderVo.setCount(BigDecimal.valueOf(0));
            return newFlowerOrderVo;
        }
        return flowerOrderVo;
    }

    @Override
    public FlowerOrderVo selectYearTotal() {
        LocalDate now = LocalDate.now();

        // 获取当年年初
        LocalDate yearStart = now.withMonth(1).withDayOfMonth(1);

        // 获取当年年末
        LocalDate yearEnd = now.withMonth(12).withDayOfMonth(31);

        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 将 LocalDate 转换为 LocalDateTime，并设置时间为 00:00:00 和 23:59:59
        LocalDateTime startTime = LocalDateTime.of(yearStart, java.time.LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(yearEnd, java.time.LocalTime.MAX);

        // 格式化 LocalDateTime
        String startTimeFormatted = startTime.format(formatter) + " 00:00:00";
        String endTimeFormatted = endTime.format(formatter) + " 23:59:59";

        FlowerOrderVo flowerOrderVo =flowerOrderMapper.selectTotal(startTimeFormatted, endTimeFormatted);
        if (flowerOrderVo ==null){
            FlowerOrderVo newFlowerOrderVo = new FlowerOrderVo();
            newFlowerOrderVo.setCount(BigDecimal.valueOf(0));
            return newFlowerOrderVo;
        }
        return flowerOrderVo;
    }

    @Override
    public FlowerOrderVo selectAllTotal() {
        FlowerOrderVo flowerOrderVo =flowerOrderMapper.selectTotal(null, null);
        if (flowerOrderVo ==null){
            FlowerOrderVo newFlowerOrderVo = new FlowerOrderVo();
            newFlowerOrderVo.setCount(BigDecimal.valueOf(0));
            return newFlowerOrderVo;
        }
        return flowerOrderVo;
    }
}
